package cn.peach.org.controller;


import cn.peach.basic.util.AjaxResult;
import cn.peach.basic.util.PageList;
import cn.peach.org.domain.Mingxi;
import cn.peach.org.query.MingxiQuery;
import cn.peach.org.service.IMingxiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/mingxi")
@RestController //@controller+responsebody,(转换json)
public class MingxiController {
    @Autowired
    private IMingxiService mingxiService;

    /**
     * 查询单条
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Mingxi queryOne(@PathVariable("id") Long id){
        return mingxiService.findById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<Mingxi> queryAll(){
       return mingxiService.findAll();
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping
    public PageList<Mingxi> queryPage(@RequestBody MingxiQuery query){
       return mingxiService.queryList(query);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            mingxiService.delete(id);
            return AjaxResult.me(); // 成功
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchdeleteByIds(@RequestBody List<Long> ids){
        try {
            mingxiService.patchDelete(ids);
            return AjaxResult.me(); // 成功
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败");
        }
    }

    /**
     * 添加
     * @param mingxi
     * @return
     */
    @PutMapping
    public AjaxResult addOruodate(@RequestBody Mingxi mingxi){
        try {
            if (mingxi.getId()==null){
                mingxiService.insert(mingxi);
            }else {
                mingxiService.update(mingxi);
            }
                return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败" + e.getMessage());
        }
    }
}
